Repository navigation
Fix hang when a ZIM creation is abandoned before finishing - #378
Conversation
37c5c04 to
9c6cf43
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #378 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 42 42
Lines 2867 2900 +33
Branches 432 438 +6
=========================================
+ Hits 2867 2900 +33 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
rgaudin
left a comment
There was a problem hiding this comment.
Haven't tested but that looks great; thanks. Was a pain.
|
@pawank925 is this ready for full review? Any reason to not link this PR to the issue so it get closed when PR is merged? |
libzim cannot cancel a creation and its worker threads call back into Python to read item content. Abandoning a Creator before finishing left those threads waiting for the GIL while the interpreter held it and joined them from the libzim destructor, deadlocking the process exit. Finish the creation with the GIL released so the workers can drain, then remove the resulting ZIM. This now happens when finishing after an error and, through an atexit sweep, when the creator is neither finished nor collected before the interpreter exits. Closes openzim#300
9c6cf43 to
fcc6750
Compare
|
Thanks @benoit74. Yes, this is ready for a full review — the fix, the CHANGELOG entry and the regression tests are all in. I've cleared the loose ends while you were asking:
On the point from #300 about deleting an in-progress ZIM being risky: that removal stays behind |
|
@benoit74 once look into this please whenever you can thanks! |
Fixes #300
The duplicate
RuntimeErroris expected in the reported program (duplicate_okdefaults toFalse). The hang happens afterwards, when the interpreter exits with theCreatorstill unfinished.libzim has no cancel API and its worker threads call back into Python (
ContentProviderWrapper.feed()) to read item content. Sampling the hung process shows:subtype_dealloc→libzim Creator::~Creator()→CreatorData::~CreatorData()→quitAllThreads()→std::thread::join(), holding the GIL;Cluster::close()→content feed→PyGILState_Ensure()→take_gil(), waiting for the GIL.Classic join/GIL deadlock. It needs pending cluster data to reproduce, which is why it only shows up once items fill a cluster. The libzim-only script from the issue did not hang because it used
with creator:, whose__exit__callsfinishZimCreation()with the GIL released.Fix
finishZimCreation()is the only way to stop libzim's threads, and libzim's binding releases the GIL around it while worker threads drain. When a creation is abandoned (can_finishisFalse),zim.Creatornow:This happens on explicit
finish()after an error, on garbage collection, and through anatexitsweep for creators that are neither finished nor collected before interpreter exit (the exact case in the report). The old docstring advice about segfaults/leaving the temp folder can be dropped.Normal finish and
workaround_nocancel=Falsebehaviour are unchanged: the ZIM is still produced when the workaround is disabled, so the "delete the in-progress ZIM" decision discussed in #300 stays opt-out.Tests
test_duplicate_on_filled_cluster_terminatesruns the reported program in a subprocess and asserts it exits (and leaves no ZIM). It times out without the fix.test_duplicate_error_discards_zim,test_garbage_collected_creation_discards_zimandtest_atexit_sweep_discards_unfinished_creationcover the explicit-finish, GC and atexit paths.test_without_workaround_keeps_zim_on_duplicatecoversworkaround_nocancel=False: the ZIM is still built despite the duplicate errors.Full
tests/zimsuite plus the rest of the suite (1104 passed, 69 skipped here), ruff,ruff formatand pyright pass, andzim/creator.pyis back to 100% line and branch coverage. The only failures locally are the pre-existingtests/imageones, which needgifsicleand friends, absent in this environment.